package top.toknow.framework;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import top.toknow.framework.dao.mapper.test.TestMapper;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @desc 应用启动时执行初始化
 * create on 2018/10/25 by watson
 */
@WebServlet(name = "appOLS", urlPatterns = "/", loadOnStartup = 1)
public class ApplicationOnLoadStart extends HttpServlet {

    public static SqlSessionFactory sqlSessionFactory;

    public void init(ServletConfig config) {
        try {
            System.out.println("开始初始化 MVC");



            System.out.println("MVC 初始化完成");



            System.out.println("开始初始化 MyBatis");
//            this.initMybatisWithXml();
            this.initMybatisWithJava();
            System.out.println("MyBatis 初始化 sqlSessionFactory 完成");

        } catch (Exception e) {
            System.out.println("MyBatis 初始化 sqlSessionFactory 异常");
            e.printStackTrace();
        }
    }

    private void initMybatisWithXml() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    private void initMybatisWithJava() throws IOException {
        //spring.primary.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
        //spring.primary.datasource.jdbc-url=jdbc:mysql://47.113.204.227:3306/ling?characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai
        //spring.primary.datasource.username=test
        //spring.primary.datasource.password=Test!123
        String driver = "com.mysql.jdbc.Drive";
        String url = "jdbc:mysql://47.113.204.227:3306/ling?characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai";
        String username = "test";
        String password = "Test!123";
        DataSource dataSource = new PooledDataSource(driver, url, username, password);
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("development", transactionFactory, dataSource);
        Configuration configuration = new Configuration(environment);
        configuration.addMapper(TestMapper.class);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置网页响应类型
        response.setContentType("text/plain;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");

        //实现具体操作
        PrintWriter out = response.getWriter();
        out.println("404错误");
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void destroy() {
        System.out.println("调用销毁方法");
    }

}
